package gov.nhso.prov.acc.apps.acc67;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import gov.nhso.prov.acc.beans.Acc67IssueBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;

@SuppressWarnings("rawtypes")
public class ACC670104 extends Utility {
	public String url = "/apps/acc67/ACC670104A.jsp";
	public ACC670104(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = request.getParameter("job");
		if (job.equals("init")) {
			this.init(request);
		} else if (job.equals("save")) {
			this.save(request);
		} else if (job.equals("update")) {
			this.update(request);
		}
	}
	private String init(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		String job = "";
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			List lsIssue = new ArrayList();
			Acc67IssueBean aib = new Acc67IssueBean();
			aib.setIssueType("M");
			lsIssue = dao.selectList(aib);
			if (lsIssue.size() <= 0) {
				job = "save";
			} else if (lsIssue.size() > 0) {
				job = "update";
				dao.select(aib);
				
			}
			request.setAttribute("aib", aib);
			request.setAttribute("job", job);
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String save(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67IssueBean aib = new Acc67IssueBean();
			this.beanUtil().copyRequest2Bean(request, aib);
			dao.insert(aib);
			this.message(request).setMsg("I", "บันทึกข้อมูลสำเร็จ");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.init(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String update(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67IssueBean aib = new Acc67IssueBean();
			this.beanUtil().copyRequest2Bean(request, aib);
			if (aib.getIssueLinkName1().equals("")) {
				aib.setIssueLinkName1("null");
			}
			if (aib.getIssueLinkUrl1().equals("")) {
				aib.setIssueLinkUrl1("null");
			}

			if (aib.getIssueLinkName2().equals("")) {
				aib.setIssueLinkName2("null");
			}
			if (aib.getIssueLinkUrl2().equals("")) {
				aib.setIssueLinkUrl2("null");
			}
			
			if (aib.getIssueLinkName3().equals("")) {
				aib.setIssueLinkName3("null");
			}
			if (aib.getIssueLinkUrl3().equals("")) {
				aib.setIssueLinkUrl3("null");
			}
			
			dao.update(aib);
			dao.printMessage();
			this.message(request).setMsg("I", "บันทึกข้อมูลสำเร็จ");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.init(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
}
